from django.db import models
from django.template.loader import render_to_string


def make_thumb(path, size=480):
    pixbuf = Image.open(path)
    width, height = pixbuf.size

    if width > size:
        delta = width / size
        height = int(height / delta)
        pixbuf.thumbnail((size, height), Image.ANTIALIAS)

        return pixbuf


# Create your models here.

class college(models.Model):
    NAME = (
        (1, u"教育学院"),
        (2, u"文学院"),
        (3, u"政治与行政学院"),
        (4, u"历史与文化学院"),
        (6, u"商学院"),
        (7, u"管理学院"),
        (8, u"数学与信息学院"),
        (9, u"物理与空间科学学院"),
        (10, u"化学与化工学院"),
        (11, u"生命与科学学院"),
        (12, u"计算机学院 "),
        (13, u"国土资源学院"),
        (14, u"学前初等教育学院"),
        (15, u"音乐学院"),
        (16, u"美术学院"),
        (17, u"体育学院"),
        (18, u"新闻传播学院"),
        (19, u"法学院"),
        (20, u"环境科学工程学院"),
        (21, u"电子与信息学院"),
        (22, u"高等职业技术学院"),
    )
    name = models.PositiveSmallIntegerField(u'学院', choices=NAME, default=1)
    title = models.CharField(u'标题', max_length=128)
    url = models.URLField(u'地址', max_length=255)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = u'首页学院管理'
        verbose_name_plural = u'首页学院管理'


class active(models.Model):
    SELECT = (
        (1, u"学院活动"),
        (2, u"部门活动"),
        (3, u"社团活动"),
    )
    select = models.PositiveSmallIntegerField(u'类型', choices=SELECT, default=1)
    title = models.CharField(u'标题', max_length=128)
    url = models.URLField(u'地址', max_length=255, primary_key=True)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = u'活动管理'
        verbose_name_plural = u'活动管理'

    def __str__(self):
        return self.title


class tinker(models.Model):
    SELECT = (
        (1, u"校级讲座"),
        (2, u"院级讲座"),
        (3, u"公益讲座"),
    )
    select = models.PositiveSmallIntegerField(u'类型', choices=SELECT, default=1)
    title = models.CharField(u'标题', max_length=128)
    url = models.URLField(u'地址', max_length=255, primary_key=True)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = u'抢票管理'
        verbose_name_plural = u'抢票管理'

    def __str__(self):
        return self.title


class job(models.Model):
    SELECT = (
        (1, u"通知公告"),
        (2, u"专场招聘"),
        (3, u"社会招聘"),
        (4, u"公招网招"),
    )
    select = models.PositiveSmallIntegerField(u'类型', choices=SELECT, default=1)
    title = models.CharField(u'标题', max_length=128)
    url = models.URLField(u'地址', max_length=255, primary_key=True)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = u'招聘就业'
        verbose_name_plural = u'招聘就业'

    def __str__(self):
        return self.title


class banner(models.Model):
    SELECT = (
        (1, u"使用"),
        (0, u"不使用"),
    )
    select = models.PositiveSmallIntegerField(u'类型', choices=SELECT, default=1)

    title = models.CharField(u'标题', max_length=128)
    url = models.URLField(u'地址', max_length=255)
    date = models.DateTimeField(auto_now_add=True)
    image = models.ImageField()

    def thumbnail(self):
        # image_path = """<img src="%s" heigtht=40>""" % (self.image.url)
        # # return image_path
        return render_to_string('thumb.html', {"banner": self})

    class Meta:
        verbose_name = u'Banner管理'
        verbose_name_plural = u'Banner管理'

    def __str__(self):
        return self.title


def upload_path_handler(instance, filename):
    # filename = "ewn1.jpg"  # 图片名称
    return "templates/img/{file}".format(file=filename)
